package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddRoleDTO;
import com.nnnu.wsnackshop.pojo.dto.RolePageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateRoleDTO;
import com.nnnu.wsnackshop.pojo.vo.RoleVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IRolesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminRolesController")
@RequestMapping("/api/admin/roles")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "管理端角色接口")
public class RolesController {

    private final IRolesService rolesService;

    @Operation(summary = "分页查询角色", description = "仅 superAdmin 或 admin 且拥有 role:read 权限可访问")
    @GetMapping
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("role:read")
    public Result<?> pageRoles(@Validated @ParameterObject RolePageDTO dto) {
        log.info("分页查询角色：{}", dto);
        return Result.success(rolesService.pageRoles(dto));
    }

    @Operation(summary = "获取角色详情", description = "仅 superAdmin 或 admin 且拥有 role:read 权限可访问")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("role:read")
    public Result<RoleVO> getRole(@PathVariable @Parameter(description = "角色ID", in = ParameterIn.PATH) Long id) {
        return Result.success(rolesService.getRoleDetail(id));
    }

    @Operation(summary = "管理员创建角色", description = "仅 superAdmin 且拥有 role:create 权限可访问")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("role:create")
    public Result<Void> createRole(@Valid @RequestBody @Parameter(description = "角色信息") AddRoleDTO dto) {
        log.info("管理员创建角色：{}", dto);
        rolesService.createRole(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新角色", description = "仅 superAdmin 且拥有 role:update 权限可访问")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("role:update")
    public Result<Void> updateRole(@Valid @RequestBody @Parameter(description = "更新角色信息") UpdateRoleDTO dto) {
        log.info("管理员更新角色：{}", dto);
        rolesService.updateRole(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "批量删除角色", description = "仅 superAdmin 且拥有 role:delete 权限可访问")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("role:delete")
    public Result<Void> deleteRoles(@RequestParam @Parameter(description = "要删除的角色ID列表", in = ParameterIn.QUERY) List<Long> ids) {
        log.info("管理员批量删除角色：ids={}", ids);
        rolesService.deleteRoles(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}


